package com.application.merecipe.server;

import java.util.ArrayList;
import java.util.List;

import com.application.merecipe.client.RecipeService;
import com.application.merecipe.shared.Recipe;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sql.database.Database;
import com.sql.database.IConnectionFactory;
import com.sql.database.IRowMapper;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class RecipeServiceImpl extends RemoteServiceServlet implements
		RecipeService {

	@SuppressWarnings("unchecked")
	@Override
	public List<Recipe> loadRecipes(String searchString) {
		IConnectionFactory connectionFactory = new MySqlConnectionFactory();
		String sql = "select * from recipe order by title";
		IRowMapper rowMapper = new RecipeRowMapper();
		List<Recipe> recipes = new ArrayList<Recipe>();
		try {
			Database database = new Database(connectionFactory);
			recipes = database.ExecuteQuery(sql, rowMapper);
		} catch (Exception e) {
			System.out.print(e.getMessage());
		}
		return recipes;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Recipe loadRecipe(Recipe recipe) {
		IConnectionFactory connectionFactory = new MySqlConnectionFactory();
		String sql = "select * from recipe where id = '" + recipe.getId() + "'";
		IRowMapper rowMapper = new RecipeRowMapper();
		List<Recipe> recipes = new ArrayList<Recipe>();
		try {
			Database database = new Database(connectionFactory);
			recipes = database.ExecuteQuery(sql, rowMapper);
		} catch (Exception e) {
			System.out.print(e.getMessage());
		}
		return recipes.get(0);

	}

	@Override
	public Recipe saveRecipe(Recipe recipe) {
		Boolean insert = true;
		IConnectionFactory connectionFactory = new MySqlConnectionFactory();
		String sql = "insert into recipe  (`title`, `no_persons`, `keywords`, `ingredients`, `description`, `image`) values"
				+ " ('"
				+ recipe.getTitle()
				+ "','"
				+ recipe.getNoPersons()
				+ "','"
				+ recipe.getKeywords()
				+ "','"
				+ recipe.getIngredients()
				+ "','"
				+ recipe.getDescription()
				+ "','" + recipe.getImage() + "')";
		if (recipe.getId() != null) {
			insert = false;
			sql = "update recipe set " + "title = '" + recipe.getTitle()
					+ "', " + "no_persons = '" + recipe.getNoPersons() + "', "
					+ "keywords = '" + recipe.getKeywords() + "', "
					+ "ingredients = '" + recipe.getIngredients() + "', "
					+ "description = '" + recipe.getDescription() + "', "
					+ "image = '" + recipe.getImage() + "' " + "where id = '"
					+ recipe.getId() + "'";
		}
		Integer noRows = 0;
		try {
			Database database = new Database(connectionFactory);
			noRows = database.ExecuteNonQuery(sql);
		} catch (Exception e) {
			System.out.print(e.getMessage());
		}
		if (insert) {
			recipe.setId(noRows);
		}
		return recipe;
	}

	@Override
	public void deleteRecipe(Recipe recipe) {
		if (recipe.getId() != null) {
			IConnectionFactory connectionFactory = new MySqlConnectionFactory();

			String sql = "delete from recipe where id = '" + recipe.getId()
					+ "'";
			try {
				Database database = new Database(connectionFactory);
				database.ExecuteNonQuery(sql);
			} catch (Exception e) {
				System.out.print(e.getMessage());
			}
		}
	}

}
